// index.js
// 获取应用实例


Page({
  data: {
   num:"0",
   op:"+"
  },

  result:null,
  isClear:false,
  // 事件处理函数
numBtn: function(e){
var num = e.target.dataset.val
if(this.data.num === "0" || this.isClear){
    this.setData({num:num});
    this.isClear=false;
}else{
this.setData({num:this.data.num + num});
}
},
opBtn:function(e){
var op = this.data.op;
var num = Number(this.data.num);
this.setData({op:e.target.dataset.val});
if(this.isClear){
  return
}
console.log(this.data.num)
this.isClear = true
if(this.result === null){
  this.result=num
  return
}
if(op === "+"){
  this.result=this.result + num
}else if(op === "-"){
  this.result=this.result - num
}else if(op === "*"){
  this.result=this.result * num
}else if(op === "/"){
  this.result=this.result / num
}else if(op === "%"){
  this.result=this.result % num
}
this.setData({num:this.result+""});
},

delBtn:function(e){
var num = this.data.num.substr(0,this.data.num.length - 1)
 this.setData({num:num === "" ? "0": num})
},
resetBtn:function(e){
this.result = null
this.isClear = false
this.setData({num: "0",op:""})
},
dotBtn:function(e){
  if(this.isClear){
    this.setData({num:"0."})
    this.isclear = false
    return
  }
  if(this.data.num.indexOf(".")>= 0){
    return
  }
  this.setData({num:this.data.num + "."})

},

})
